package com.blockchain.front.util;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.blockchain.util.StringHelper;
import com.ucpaas.sms.client.AbsRestClient;
import com.ucpaas.sms.client.JsonReqClient;

public class SendMsg {
	/*public static void sendMsg(String content, String phone) {
		System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
		System.setProperty("sun.net.client.defaultReadTimeout", "30000");
		StringBuffer buffer = new StringBuffer();
		String encode = "UTF-8";
		String username = "zichanhui";
		String password_md5 = "7240835ee7502289c76f5e9bd4cddb8d";
		String mobile = phone;
		String apikey = "c7031c1f1694c9adab6649982d702597";
		try {
			String contentUrlEncode = URLEncoder.encode(content, encode);
			buffer.append("http://m.5c.com.cn/api/send/index.php?username=" + username + "&password_md5=" + password_md5
					+ "&mobile=" + mobile + "&apikey=" + apikey + "&content=" + contentUrlEncode + "&encode=" + encode);
			URL url = new URL(buffer.toString());
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Connection", "Keep-Alive");
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String result = reader.readLine();
			System.out.printf("发送人：%s/t返回结果:%s/t/n", phone, result);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}*/
	
	/**
	 * @param smsTemplateId -短信模板id
	 * @param phone --手机号
	 * @param param --短信内容填充的参数，用逗号分隔每个参数（如："a,b,c"）
	 * */
	public static void sendMsg(String smsTemplateId,String param,String phone) {
		AbsRestClient ac =  new JsonReqClient();
		String result = "";

		try {
			result=ac.templateSMS(AbsRestClient.getValueByKey("accountSid"), 
					AbsRestClient.getValueByKey("authToken"), 
					AbsRestClient.getValueByKey("verifySMSAppId"), 
					smsTemplateId, phone, param);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		System.out.println("Response content is: " + result);
	}

	public static void main(String[] args) throws IOException {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		StringBuilder param = new StringBuilder();
		BigDecimal je = new BigDecimal("35.2");
		param.append(StringHelper.truncation("13410791091")).append(",")
			 .append(df.format(new Date())).append(",")
			 .append("BTB").append(",")
			 .append(je).append(",")
			 .append("lastparam!");
		
		//测式二
		SendMsg.sendMsg(AbsRestClient.getValueByKey("N_SMS_TX_JE"),param.toString(),"13410791091");
	}
}
